package com.resourcestore.utils.tag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.resourcestore.action.MajorFileSrcCateAction;
import com.resourcestore.model.MajorFileSrcCate;

public class TreeSelector extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private int majorFileCateId;

	private String type;

	private String selectName;

	private String notNeedValidation;

	private int delMajorFileSrcCateId;// 在某些修改场景，与所修改项相同的项及其子项都需要从列表中删除
	
	private String target;

	public int doEndTag() throws JspException {

		JspWriter out = pageContext.getOut();

		List<MajorFileSrcCate> listMFSC = new ArrayList<MajorFileSrcCate>();
		List<MajorFileSrcCate> delList = new ArrayList<MajorFileSrcCate>();

		MajorFileSrcCateAction majorFileSrcCateAction = new MajorFileSrcCateAction();
		MajorFileSrcCate majorFileSrcCate = new MajorFileSrcCate();
		MajorFileSrcCate delMFSC= new MajorFileSrcCate();
		
		majorFileSrcCate.setMajorFileCateParentId(-1);

		try {
			StringBuffer sb = new StringBuffer();
			//sb.append(getNotNeedValidation() == "false" ? "" : "class=\"validate['required']\"");
			
			out.println("<select name=\"" + type + "." + getSelectName() + "\" id=\""
					+ getSelectName() + "\" " + sb.toString() + ">");
			if(target.equals("search")){
				out.println("<option value=\"\">全部</option>");
			}
			out.println("<option value=\"-1\">根分类（专业文件资源）</option>");

//			if (delChapterId != 0) {
//				delChpt.setChapterId(delChapterId);
//				delList = chapterAction.listAllSubNode(delChpt);
//			}

			listMFSC = majorFileSrcCateAction.recursiveListMajorFileSrcCateTree(majorFileSrcCate);
			
			for (MajorFileSrcCate eachMFSC : listMFSC) {// 将所有章节打印在页面上
				
				//格式化列表
//				String blankString = "";		
//				for(int i=1;i<eachMFSC.getDepth();i++){
//					blankString = blankString + "&nbsp;&nbsp;";
//				}
//				eachMFSC.setMajorFileCateName(blankString+ eachMFSC.getMajorFileCateName());
				eachMFSC.setMajorFileCateName("|--"+ eachMFSC.getMajorFileCateName());
				for(int i=0;i<eachMFSC.getDepth();i++){
					eachMFSC.setMajorFileCateName("&nbsp;&nbsp;&nbsp;&nbsp;"+ eachMFSC.getMajorFileCateName());
				}
//				int blankNum = eachMFSC.getMajorFileCateId();
//				while (blankNum / 10 > 10) {
//					eachMFSC.setMajorFileCateName("&nbsp;&nbsp;&nbsp;&nbsp;"
//							+ eachMFSC.getMajorFileCateName());
//					blankNum = blankNum / 100;
//				}

				// 得到了chapterId值，说明是修改，修改与新建的唯一不同是多一句selected="selected"
				// 只有一个selected="selected"
				String cateRemark = "";
				if(eachMFSC.getIsPreCategory()==1){
					cateRemark = "（预置）";
				}else if(eachMFSC.getIsPreCategory()==0){
					cateRemark = "（"+eachMFSC.getMajorName()+"）";
				}
				
				if (0 != majorFileCateId && majorFileCateId == eachMFSC.getMajorFileCateId()) {
					out.println("<option value=\"" + eachMFSC.getMajorFileCateId()
							+ "\" selected=\"selecetd\" >" + eachMFSC.getMajorFileCateName()+cateRemark
							+ "</option>");
				} else {
					out.println("<option value=\"" + eachMFSC.getMajorFileCateId() + "\">"
							+ eachMFSC.getMajorFileCateName() +cateRemark+ "</option>");
				}

			}
			out.println("</select>");
		} catch (IOException e) {
			e.printStackTrace();
			return super.SKIP_BODY;
		}

		return super.EVAL_PAGE;
	}

	public int getMajorFileCateId() {
		return majorFileCateId;
	}

	public void setMajorFileCateId(int majorFileCateId) {
		this.majorFileCateId = majorFileCateId;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getSelectName() {
		if (null == selectName) {
			selectName = "majorFileSrcCateId";
		}
		return selectName;
	}

	public void setSelectName(String selectName) {
		this.selectName = selectName;
	}

	public String getNotNeedValidation() {
		return notNeedValidation;
	}

	public void setNotNeedValidation(String notNeedValidation) {
		this.notNeedValidation = notNeedValidation;
	}

	public int getDelMajorFileSrcCateId() {
		return delMajorFileSrcCateId;
	}

	public void setDelMajorFileSrcCateId(int delMajorFileSrcCateId) {
		this.delMajorFileSrcCateId = delMajorFileSrcCateId;
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}
	
}
